package com.cn.goodsadmin.listener;


import com.cn.goodsadmin.config.BaseConfig;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class BeanFactoryServlet extends HttpServlet {

    private static final long serialVersionUID = 0xa47bd810b596ef55L;
    private static WebApplicationContext webContext = null;
    //定时发送卡卷信息
    public static ScheduledExecutorService Cardtimer;
    //定时拉取acessToken的timer
    public static ScheduledExecutorService accesTokenTimer;
    //定时模版任务
    public static ScheduledExecutorService scheduledExecutorService;
    static byte[] lock = new byte[0];

    /**
     * 销毁
     */
    public void destroy() {
        super.destroy();
    }

    public static BeanFactory getWebContext() {
        return webContext;
    }

    public void init(ServletConfig config) throws ServletException {
        try {
            System.out.print("init come in");
            webContext = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
            //启动定时任务
            accesTokenTimer = Executors.newScheduledThreadPool(1);
            Cardtimer = Executors.newScheduledThreadPool(1);
            AcessGetTask acessGetTask = new AcessGetTask(BaseConfig.APPID, BaseConfig.secretkey);
            accesTokenTimer.scheduleAtFixedRate(acessGetTask, 0, 5400, TimeUnit.SECONDS);
            scheduledExecutorService = Executors.newScheduledThreadPool(5);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
